package com.xunyin.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtil {

    /**
     * 时间戳转为日期
     *
     * @param timestamp
     * @return
     */
    public static String timestampToDate(Long timestamp) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d1 = new Date(timestamp);
        String d2 = simpleDateFormat.format(d1);
        return d2;
        //return parseStrint2Date(d2,"yyyy-MM-dd HH:mm:ss");
/*		Date date = null;
		try {
			date = simpleDateFormat.parse(d2);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;*/
    }

    public static Date parseStrint2Date(String dateStr, String pattern) {
        Date date = null;
        if (StringUtils.isNotBlank(dateStr)) {
            DateFormat df = new SimpleDateFormat(pattern);
            try {
                date = df.parse(dateStr.toString());
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return date;
    }

    public static String parseDate2String(Date date, String pattern) {
        String dateStr = "";
        if (date != null) {
            DateFormat df = new SimpleDateFormat(pattern);
            dateStr = df.format(date);
        }
        return dateStr;
    }

    public static String appendTimeFrom(String dateStr) {
        String result = " 00:00:00.0";
        if (StringUtils.isNotBlank(dateStr)) {
            return dateStr + result;
        }
        return "";
    }

    public static String appendTimeFromSecond(String dateStr) {
        String result = " 00:00:00";
        if (StringUtils.isNotBlank(dateStr)) {
            return dateStr + result;
        }
        return "";
    }

    public static String appendTimeTo(String dateStr) {
        String result = " 23:59:59.999";
        if (StringUtils.isNotBlank(dateStr)) {
            return dateStr + result;
        }
        return "";
    }

    public static String appendTimeToSecond(String dateStr) {
        String result = " 23:59:59";
        if (StringUtils.isNotBlank(dateStr)) {
            return dateStr + result;
        }
        return "";
    }

    /**
     * @param dateStr yyyy-MM-dd格式的日期
     * @return
     */
    public static Date parseStrint2DateFrom(String dateStr) {
        Date date = null;
        if (StringUtils.isNotBlank(dateStr)) {
            dateStr = appendTimeFrom(dateStr);
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
            try {
                date = df.parse(dateStr);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return date;
    }

    /**
     * @param dateStr yyyy-MM-dd格式的日期
     * @return
     */
    public static Date parseStrint2DateTo(String dateStr) {
        Date date = null;
        if (StringUtils.isNotBlank(dateStr)) {
            dateStr = appendTimeTo(dateStr);
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
            try {
                date = df.parse(dateStr);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return date;
    }

    /**
     * @param dateStr yyyy-MM-dd格式的日期
     * @return
     */
    public static Date parseStringformatDate(String dateStr, String format) {
        Date date = null;
        if (StringUtils.isNotBlank(dateStr)) {
            dateStr = appendTimeTo(dateStr);
            DateFormat df = new SimpleDateFormat(format);
            try {
                date = df.parse(dateStr);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return date;
    }

    /**
     * @param dateStr 指定年月日格式
     * @return
     */
    public static Date parseStrint2DateTo(String dateStr, String yyMMdd) {
        Date date = null;
        if (StringUtils.isNotBlank(dateStr)) {
            dateStr = appendTimeTo(dateStr);
            DateFormat df = new SimpleDateFormat(yyMMdd + " HH:mm:ss.S");
            try {
                date = df.parse(dateStr);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return date;
    }

    /**
     * 获取指定时间段的日期列表
     *
     * @param dateFrom
     * @param dateTo
     * @param pattern
     * @return
     */
    public static List<String> getDateRangeList(Date dateFrom, Date dateTo, String pattern) {
        List<String> dateRangeList = new ArrayList<String>();
        Date date = dateFrom;
        Calendar calendar = Calendar.getInstance();
        while (date.compareTo(dateTo) <= 0) {
            dateRangeList.add(parseDate2String(date, pattern));
            calendar.setTime(date);
            calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + 1);
            date = calendar.getTime();
        }
        return dateRangeList;
    }

    /**
     * 获取当前应用服务器的系统日期时间
     *
     * @return
     */
    public static Date getCurrentDateTime() {
        return new Date(System.currentTimeMillis());
    }


    /**
     * 获取当前应用服务器的系统日期时间
     *
     * @return 日期时间字符串，格式：yyyy-MM-dd HH:mm:ss
     */
    public static String getCurrentDateTimeStr() {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.format(new Date(System.currentTimeMillis()));
    }

    public static String getCurrentDateFormatStr(String format) {
        DateFormat df = new SimpleDateFormat(format);
        return df.format(new Date(System.currentTimeMillis()));
    }

    /**
     * 获取当前应用服务器的系统日期时间
     *
     * @return 日期时间字符串，格式：yyyyMMddHHmmss
     */
    public static String getCurrentDateTime14() {
        DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        return df.format(new Date(System.currentTimeMillis()));
    }

    /**
     * 获取当前应用服务器的系统日期时间
     *
     * @return 日期时间字符串，格式：yyyyMMdd
     */
    public static String getCurrentDate8() {
        DateFormat df = new SimpleDateFormat("yyyyMMdd");
        return df.format(new Date(System.currentTimeMillis()));
    }

    public static String getFormatString(String date, String srcpattern, String destpattern) {
        String retstr = "";
        try {
            Date d = new SimpleDateFormat(srcpattern).parse(date);
            retstr = new SimpleDateFormat(destpattern).format(d);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return retstr;
    }

    /**
     * 增加或减少时间
     *
     * @param date
     * @param calendarField 增加作用域映射值   如：年份为 Calendar.YEAR = 1
     * @param amount        正数为增加，负数为减少
     * @return
     */
    public static Date addDate(Date date, int calendarField, int amount) {
        if (null == date) {
            return date;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        //calendar.roll(calendarField, amount);
        calendar.add(calendarField, amount);
        return calendar.getTime();

    }


    /**
     * 获取两个日期之间的日期
     *
     * @param start 开始日期
     * @param end   结束日期
     * @return 日期集合
     */
    public static List<Date> getBetweenDates(Date start, Date end) {
        List<Date> result = new LinkedList<Date>();
        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(start);
        //tempStart.add(Calendar.DAY_OF_YEAR, 1);

        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(end);
        while (tempStart.before(tempEnd) || tempStart.equals(tempEnd)) {
            result.add(tempStart.getTime());
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
        }
        return result;
    }

    /**
     * 获取两个日期之间的日期
     *
     * @param start 开始日期
     * @param end   结束日期
     * @return 日期集合
     */
    public static List<String> getBetweenDatesStr(Date start, Date end, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        List<String> result = new ArrayList<>();
        for (Date date : getBetweenDates(start, end)) {
            result.add(simpleDateFormat.format(date));
        }
        return result;
    }


    public static void main(String[] args) {

        //System.out.println(addDate(new Date(), Calendar.MINUTE, -10));
        //System.out.println(timestampToDate(1661657159000L));
        //Instant instant = Instant.now();
        System.out.println(parseStrint2Date("2024-07-29 17:56", "yyyy-MM-dd HH:mm"));

    }

    /**
     * @param day
     * @return
     */
    public static String getWeekDay(String day) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        calendar.setTime(sdf.parse(day));
        String[] weekDays = new String[]{"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        return weekDays[calendar.get(Calendar.DAY_OF_WEEK) - 1];
    }


    /**
     * 秒转换为指定格式的日期
     * * @param second
     * * @param patten
     * * @return
     */
    public static String secondToDate(long second, String patten) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(second * 1000);//转换为毫秒
        Date date = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat(patten);
        String dateString = format.format(date);
        return dateString;
    }

    /*** 返回日时分秒
     *  * @param second
     *  * @return
     *  */
    public static String secondToTime(long second) {
        long days = second / 86400;//转换天数
        second = second % 86400;//剩余秒数
        long hours = second / 3600;//转换小时数
        second = second % 3600;//剩余秒数
        long minutes = second / 60;//转换分钟
        second = second % 60;//剩余秒数
        if (0 < days) {
            return days + "天 " + hours + "小时 " + minutes + "分 " + second + "秒";
        } else {
            return hours + "小时 " + minutes + "分 " + second + "秒";
        }
    }

    public static String getDateDiff(Date d1, Date d2) {
        try {
            long diff = d1.getTime() - d2.getTime();//这样得到的差值是微秒级别
            long days = diff / (1000 * 60 * 60 * 24);

            long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
            long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);

            return days + "天" + hours + "小时" + minutes + "分钟";
        } catch (Exception e) {
            return "0";
        }
    }

    public static int getDateDiffLong(Date d1, Date d2) {
        try {
            int diff = (int) (d1.getTime() - d2.getTime()) / 1000;
            return diff;
        } catch (Exception e) {
            return 0;
        }
    }

    public static String getDateDiffByString(String d1, String d2) {
        try {
            Date dldate = parseStrint2Date(d1, "yyyy-MM-dd");
            Date d2date = parseStrint2Date(d2, "yyyy-MM-dd");
//			if(d2date.after(dldate)){
//				return "0";
//			}else{
            long diff = dldate.getTime() - d2date.getTime();//这样得到的差值是微秒级别
            long days = diff / (1000 * 60 * 60 * 24);
            return days + "";
//			}
        } catch (Exception e) {
            return "0";
        }
    }

}
